/*
 * @Author: your name
 * @Date: 2024-04-03 20:43:31
 * @LastEditTime: 2024-04-04 22:03:37
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \undefinedc:\Users\ljg\.leetcode\438.找到字符串中所有字母异位词.cpp
 */
/*
 * @lc app=leetcode.cn id=438 lang=cpp
 *
 * [438] 找到字符串中所有字母异位词
 */

// @lc code=start
class Solution {
public:
    vector<int> findAnagrams(string s, string p) {
        if(s.size() < p.size()){
            return {};
        }
        vector<int> res;
        vector<int> s_count(26);
        vector<int> p_count(26);
        int s_len = s.size(), p_len = p.size(); 
        for(int i = 0; i< p_len;i++){
            ++s_count[s[i]-'a'];
            ++p_count[p[i]-'a'];
        }
        if(s_count == p_count){
            res.emplace_back(0);
        }
        for(int i = p_len; i< s_len; i++){
            ++ s_count[s[i]-'a'];
            -- s_count[s[i-p_len]-'a'];
            if(s_count == p_count){
                res.emplace_back(i-p_len+1);
            }
        }
        return res;
    }
};
// @lc code=end
// 时间超过了！
